<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class Price extends Model
{   
	function getProPrice($data=[],$lang,$where=[],$page=0,$limit=0)
	{	
		// 价格分类 
        $joinprice_type = [
                            ['multi_lang mu','zu.mkey = mu.mkey',"LEFT"],
                        ];
        $price_type = getSearch('price_type','zu',['zu.id,zu.code,zu.mkey,zu.rate,zu.moq,zu.discount,zu.rate,mu.content type_name'],['mu.lang'=>$lang],$joinprice_type,'zu.drank asc');
        $price_type_num = count($price_type);

        // dump($price_type);die;
        // 获取产品的条件（pro.is_item=0 AND is_sale=1 AND pulloff_day < today AND status=NORMAL AND del_at=0）
        $where['p.is_item'] = 0;
        $where['p.is_sale'] = 1;
        // $where['p.pulloff_day'] = ['<',strtotime(date("Y-m-d"))];
        $where['p.status'] = 1;
        $where['p.del_at'] = 0;
        $where['pm.lang'] = $lang;
        $priceData = [];
		if(!empty($data)){
			foreach ($data as $k1 => $v1) {
				if($v1['prochar_id'] == 0){
					$res = $this->getProPrice_($where,$v1['pro_id']);
					foreach ($res as $key => $value) {
						array_push($priceData, $value);
					}
				}else{
                    $result =  $this->getProPrice_($where,$v1['pro_id'],$v1['prochar_id']);
                    if(!empty($result)) $priceData[] = $result[0];
				}
			}
		}else{
			$priceData = $this->getProPrice_($where);
		}
		$pageSum = count($priceData);
        if($page && $limit){
            $priceData = array_slice($priceData, ($page-1)*$limit,$limit);
        }


        foreach ($priceData as $k => $v) {
            $proSale = Db::name('pro_sale')->where(['pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id']])->find();
            // CIF
            $priceData[$k]['cif'] = $proSale ? $proSale['cif'] : 0;
            // 运费
            $priceData[$k]['fare'] = $proSale ? $proSale['tran_fee'] : 0;
            // 运费/CIF
            $priceData[$k]['fare_cif'] = $proSale ? round($priceData[$k]['fare'] / $priceData[$k]['cif'],2) : 0;

            // 库存
            $priceData[$k]['reserve_ctn'] = 10;
            $priceData[$k]['reserve_pcs'] = 2000;

            $priceData[$k]['time'] = getTimeDates($v['create_at'],$lang);
            $priceData[$k]['nickname'] = getinfo($v['create_id'], 'admin')['nickname'];//编辑者

            $priceData[$k]['img_url'] = getImgUrl($v['pro_id'],48,'prochar',$v['prochar_id']);

            // 定价--价格分类内容
            $price_id = db::name('price')->where(['pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id']])->order('id desc')->limit(1)->value('id');
            // dump($price_id);die;

            foreach ($price_type as $keyprice_type => $valueprice_type) {
                $priceData[$k]['price_value'][$keyprice_type]['amount'] = 0;
                $priceData[$k]['price_value'][$keyprice_type]['moq'] = 0;
                $priceData[$k]['price_value'][$keyprice_type]['discount_range'] = 0;
                $priceData[$k]['price_value'][$keyprice_type]['price_type_id'] = $valueprice_type['id'];
                $priceData[$k]['price_value'][$keyprice_type]['type_name'] = $valueprice_type['type_name'];
                $priceData[$k]['price_value'][$keyprice_type]['code'] = $valueprice_type['code'];
                $priceData[$k]['price_value'][$keyprice_type]['priceTypeMoq'] = $valueprice_type['moq'];
                $priceData[$k]['price_value'][$keyprice_type]['priceTypeDiscount'] = $valueprice_type['discount'];
                $priceData[$k]['price_value'][$keyprice_type]['priceTypeAmount'] = $valueprice_type['rate'];
            }
            if($price_id){
                foreach ($price_type as $keyprice_typee => $valueprice_typee) {
                    $priceValue = getDatas('price_value',['*'],['price_id'=>$price_id,'price_type_id'=>$valueprice_typee['id']]);
                    // dump($valueprice_typee['id']);
                    if(!empty($priceValue)){
                        $priceData[$k]['price_value'][$keyprice_typee] = $priceValue['0'];
                        $priceData[$k]['price_value'][$keyprice_typee]['type_name'] = $valueprice_typee['type_name'];
                        $priceData[$k]['price_value'][$keyprice_typee]['code'] = $valueprice_typee['code'];
                        $priceData[$k]['price_value'][$keyprice_typee]['priceTypeMoq'] = $valueprice_typee['moq'];
                        $priceData[$k]['price_value'][$keyprice_typee]['priceTypeDiscount'] = $valueprice_typee['discount'];
                        $priceData[$k]['price_value'][$keyprice_typee]['priceTypeAmount'] = $valueprice_typee['rate'];
                    }
                }
            }

            $priceData[$k]['nickname'] = getinfo($v['create_id'], 'admin')['nickname'];//编辑者
            $priceData[$k]['time'] = getTimeDates($v['create_at'],$lang);
        }
        return ['price_list'=>$priceData,'price_type'=>$price_type,'pageSum'=>$pageSum,'page'=>$page == 0 ? 1 : $page ];

	}


	function getProPrice_($where,$pro_id=0,$prochar_id=0)
	{	
		if($pro_id != 0){
			if($prochar_id == 0){
				$where['zu.pro_id'] = $pro_id;
			}else{
				$where['p.id'] = $pro_id;
				$where['zu.id'] = $prochar_id;
			}
		}
		
		$join = [
                    ['pro p','zu.pro_id = p.id',"LEFT"],        
                    ['multi_lang pm','p.mkey = pm.mkey',"LEFT"],
                    ['pro_brand b','b.id = p.brand_id','LEFT'],
                    ['pro2cate pt','p.id = pt.pro_id','LEFT']
                ];
        $field = [
            'p.id pro_id,p.mkey,p.brand_id,p.create_at,p.create_id,b.name brand_name,pm.content pro_name,zu.id prochar_id,zu.model,zu.ctn_size,zu.ctn_kgs,zu.ctn_m3,zu.package_qty,zu.qty prochar_qty'
        ];
        // $field = [
        //     'p.id pro_id,p.mkey,p.brand_id,p.create_at,p.create_id,zu.id prochar_id,zu.model,zu.ctn_size,zu.ctn_kgs,zu.ctn_m3,zu.package_qty'
        // ];
        $priceData =  Db::name('prochar zu')
            ->join($join)
            ->field($field)
            ->where($where)
            ->order('zu.id desc')
            // ->fetchSql()
            ->select();
         return $priceData;
	}


	// 获取系统变量，以计算运费/CIF
    function getSysVariable()
    {
        $sysVariable = getDatas('sys_variable',['*'],'1=1','id asc');
        foreach ($sysVariable as $k => $v) {
            $res = Db::name('sys_variable_value')->where('variable_id',$v['id'])->order('create_at desc')->value('value');
            $sysVariableAndValue[$v['name']] = $sysVariable[$k];
            $sysVariableAndValue[$v['name']]['value'] = $res == null ? 0 : $res;

        }
        return $sysVariableAndValue;
    }
}
